﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeManageService
{
    public class UserManager
    {
        public List<User> Users;

        UserDBManager dBManager = new UserDBManager();

        public UserManager()
        {
            Users = new List<User>();
            Users = GetRecentUsers();
        }
        /*
         * bool Login 登陆
         * bool Register 注册
         * String FindPassword 找回密码
         * List<User> GetRecentUsers 获取最近登陆账号
         */

        public bool Login(User user)
        {
            User confirmUser = dBManager.GetUser(user.UserName);
			if (confirmUser != null && user.PassWord == confirmUser.PassWord) {
				user.Score = confirmUser.Score;
				return true;
			}
			else return false;
        }

        public User GetUser(string username)
        {
            UserDBManager dBManager = new UserDBManager();
            return dBManager.GetUser(username);
        }


        public bool Register(ref User user)
        {
            if (dBManager.GetUser(user.UserName)==null)
            {                
                dBManager.addUser(user);
                return true;
            }
            return false;
        }

        public String FindPassword(string UserName)
        {
            User confirmUser = dBManager.GetUser(UserName);
            if (confirmUser != null)
            {
                return confirmUser.PassWord;
            }
            else
            {
                return "";
            }
        }

        public List<User> GetRecentUsers()
        {
            List<User> users = new List<User>();
            users = dBManager.GetAllUsers();
            return users;
        }

        public void SetLoginWay(User user)
        {
            dBManager.modifyUser(user);
        }

        public void ModifyUser(User user)
        {
            dBManager.modifyUser(user);
        }
    }
}
